package com.kmxd.ams.controller.archive;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.archive.IArchiveClassifySvc;
import com.kmxd.ams.client.archive.dto.ArchiveClassifyAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveClassifyEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveClassifyQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveClassifyListVO;
import com.kmxd.ams.client.archive.vo.ArchiveClassifyVO;
import com.kmxd.ams.client.arrange.dto.BaseQueryDTO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 整理规则-分类管理 前端控制器
 *
 * @author nickbi
 * @since 2022-07-19
 */
@RestController
@Tag(name = "整理规则-分类管理")
@RequestMapping("/archive/archiveClassify")
@AllArgsConstructor
public class ArchiveClassifyCtl {

  private IArchiveClassifySvc archiveClassifyService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(
      module = "整理规则-分类管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "分页查询")
  public R<Page<ArchiveClassifyListVO>> selectPage(Page reqPage, ArchiveClassifyQueryDTO req) {
    return R.success(archiveClassifyService.selectPage(reqPage, req));
  }

  /** 树形列表 */
  @Operation(summary = "树形列表")
  @GetMapping("/tree")
  @AutoLog(
      module = "整理规则-分类管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "树形列表")
  public R<List<Tree<String>>> tree(ArchiveClassifyQueryDTO req) {
    return R.success(archiveClassifyService.tree(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @SaCheckPermission("archive:archiveClassify:add")
  @AutoLog(module = "整理规则-分类管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<ArchiveClassifyVO> add(@Validated @RequestBody ArchiveClassifyAddDTO req) {
    ArchiveClassifyVO vo = archiveClassifyService.add(req);
    return R.success(vo);
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @SaCheckPermission("archive:archiveClassify:edit")
  @AutoLog(module = "整理规则-分类管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "修改")
  public R<ArchiveClassifyVO> edit(@Validated @RequestBody ArchiveClassifyEditDTO req) {
    ArchiveClassifyVO vo = archiveClassifyService.edit(req);
    return R.success(vo);
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @SaCheckPermission("archive:archiveClassify:remove")
  @AutoLog(module = "整理规则-分类管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    archiveClassifyService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "整理规则-分类管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<ArchiveClassifyVO> view(@RequestParam Long id) {
    return R.success(archiveClassifyService.view(id));
  }

  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "整理规则-分类管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(ArchiveClassifyQueryDTO req, HttpServletResponse response) {
    String filename = "整理规则-分类管理列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(ArchiveClassifyListVO.class)
        .sheet("整理规则-分类管理列表")
        .doWrite(archiveClassifyService.selectList(req));
  }

  /** 查询门类树 */
  @Operation(summary = "查询门类树")
  @GetMapping("/listClassifyTree")
  @AutoLog(module = "整理规则", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询门类树")
  public R<List<Tree<String>>> listClassifyTree(BaseQueryDTO req) {
    return R.success(archiveClassifyService.listClassifyTree(req));
  }

  /** 获取自动增加排序 */
  @Operation(summary = "获取自动增加排序")
  @PostMapping("/getAutoIndex")
  @AutoLog(module = "整理规则-获取自动增加排序", type = LogTypeEnum.SYSTEM, title = "获取自动增加排序")
  public R<ArchiveClassifyVO> getAutoIndex(@RequestBody ArchiveClassifyEditDTO req) {
    return R.success(archiveClassifyService.getAutoIndex(req));
  }
}
